package cn.edu.scau.cmi.hekaimin.multiton;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextInputDialog;
import javafx.scene.control.Alert.AlertType;

import java.util.ArrayList;
import java.util.Optional;

import javafx.event.ActionEvent;

public class MultitonController {
	@FXML
	private Button btn_1;
	@FXML
	private Button btn_2;
   
	private ArrayList<Marshal> marshals = null;
	private ArrayList<String> names = null;
	// Event Listener on Button[#btn_1].onAction
	@FXML
	public void eventAction1(ActionEvent event) {
		for(int i = 1; ; i++) {
		    if(marshals != null && marshals.size() >= 10) {
		    	Alert alert = new Alert(AlertType.INFORMATION);
				alert.titleProperty().set("信息");
				alert.headerTextProperty().set("创建十大元帅已经完成!");
				alert.showAndWait();
				break;
		    }  	
			TextInputDialog dialog = new TextInputDialog();
			dialog.setTitle("创建十大元帅");			
			dialog.setHeaderText("请输入第"+ i +"个元帅的名字");
			Optional result = dialog.showAndWait();
			if (result.isPresent()) {
             marshals = Marshal.addInstance(result.get().toString());
		    }
			names = Marshal.getNames();
	    }
	}
	// Event Listener on Button[#btn_2].onAction
	@FXML
	public void eventAction2(ActionEvent event) {
		// TODO Autogenerated
		Alert alert = new Alert(AlertType.INFORMATION);
		alert.titleProperty().set("信息");
		if(marshals == null) {
			alert.headerTextProperty().set("请先创建十大元帅!");
		}else {
			StringBuilder str = new StringBuilder();
			for(int i = 0; i < marshals.size();i++) {
				str.append("第"+ (i+1) + "位为:" +names.get(i)+ "\n");
			}
			alert.headerTextProperty().set(str.toString());
		}
		alert.showAndWait();
	}
}
